package com.swn.demo.addreess.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swn.common.util.DbHelper;
import com.swn.common.util.UUIDUtil;
import com.swn.demo.address.service.Demo3Service;
@Service("demo3Service")
public class Demo3ServiceImpl implements Demo3Service{

	private static Logger logger = Logger.getLogger("Demo3Service.class");
	@Autowired
	private DbHelper dbHelper;
	
	@Override
	public String saveOrUpdateAddress(Map<String, Object> map) {
		try {
			if(map.containsKey("is_default")&&"1".equals(map.get("is_default").toString())){
				String sql="update z_address set is_default=0 where customer_id=:customer_id and is_default=1";
				dbHelper.update(sql, map);
			}
			if(!map.containsKey("id")){
				String id = UUIDUtil.getUUID();
				map.put("id", id);
				dbHelper.saveMap(map, "z_address");
				return id;
			}else{
				dbHelper.updateMap(map, "z_address");
				return "";
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new RuntimeException();
		}
	}

	@Override
	public List<Map<String, Object>> queryAllAddress(String user_id) {
		StringBuilder sql = new StringBuilder();
		sql.append("select id,receiver,receiver_pho,receipt_province,receipt_city,receipt_county,receipt_address")
		   .append(",is_default from z_address")
		   .append(" where customer_id='").append(user_id).append("'");
		return dbHelper.queryForList(sql.toString(), null);
	}
    
	/**
	 * date:2018-08-09
	 * author:
	 * 
	 */
	@Override
	public int deleteAddress(String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		return dbHelper.delete(map, "z_address");
	}

}
